# -*- coding: utf-8 -*-

__author__ = 'bilibili'

import json
import types

class spot_config:
    # FUNCTION: __init__(self, string)
    #   the constructor of the class spot_config
    # ARGS:
    #   @file_name: the json file name for the configuration
    def __init__(self, file_name):
        f = file(file_name)
        self.m_raw_dict = json.load(f)
        f.close()

    # FUNCTION: debug_output(self)
    #   the output function for debugging
    def debug_output(self):
        for (d, x) in self.m_raw_dict.items():
            print "key:" + d + ", value:" + str(x)
#            if type(x) == types.IntType:
#                print "key:" + d + ", value:" + str(x)

    # FUNCTION: get_attr(self, string, string)
    #   to get the content from json dictionary
    #   the config json for this app has two level most
    # ARGS:
    #   @key_name1: the key to get the content for first level
    #   @key_name2: the key to get the sub-content for the second leve
    # RETURN:
    #   string: the content corresponding to the attribute(s)
    #   None: no such attribute(s) exist
    def get_attr(self, key_name1, key_name2=None):
        if type(self.m_raw_dict[key_name1]) == types.DictType:
            if key_name2 == None or not self.m_raw_dict[key_name1].has_key(key_name2):
                return None
            return self.m_raw_dict[key_name1][key_name2]
        return self.m_raw_dict[key_name1]

    # FUNCTION: get_dict(self)
    #   to get the raw dictionary
    # RETURN:
    #   dict: the raw dictionary
    def get_dict(self):
        return self.m_raw_dict

def test_spot_config():
    analyzer = spot_config('C:\\svn_repo\\SPOT\\trunk\\DashboardDataService\\resources\\AM_Report.config.json')
    print analyzer.get_attr('FY13 Bookings', 'PM1 ')

if __name__ == '__main__':
    test_spot_config()